package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.Adress;
import com.elite.service.AdressService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by hbl on 2017/1/23.
 */
@RestController
@RequestMapping("/address")
public class AdressController {
    //引入log4j进行打印
    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    @Autowired
    private AdressService adressService;
    String result;


    /**
     * 将收货地址放入数据库中
     * @param userId
     * @param goodsAddress
     * @return
     */
    @RequestMapping(value = "/setInfo",method = RequestMethod.POST)
    @ResponseBody
    public void setInfo(Integer userId, String goodsAddress){
        Adress adress = new Adress();
        adress.setUserId(userId);
        adress.setGoodsAdress(goodsAddress);
        adressService.addAddress(adress);
    }

    /**
     * 将改用户已有的地址输出
     * @param userId
     * @return
     */
    @RequestMapping(value = "/selectByUserId",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectByUserId(Integer userId){
        List<Adress> adresses = adressService.selectAddressById(userId);
        return JSON.toJSON(adresses).toString();
    }

    /**
     * 通过addressid搜索
     * @param addressId
     * @return
     */
    @RequestMapping(value = "/selectByKey",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectByKey(Integer addressId){
        Adress adress = adressService.selectByPrimaryKey(addressId);
        return JSON.toJSON(adress).toString();
    }

    /**
     * 删除一条地址数据
     * @param addressId
     * @return
     */
    @RequestMapping(value = "/deleteByAddressId")
    @ResponseBody
    public void deleteByAddressId(Integer addressId){
        adressService.delete(addressId);
    }

    /**
     * 实现修改地址功能
     * @param adressId
     * @param goodsAdress
     */
    @RequestMapping(value = "/updateGoodsAddress",method = RequestMethod.POST)
    @ResponseBody
    public void updateGoodsAddress(Integer adressId,Integer userId,String goodsAdress){
        Adress adress = new Adress();
        adress.setAdressId(adressId);
        adress.setUserId(userId);
        adress.setGoodsAdress(goodsAdress);
        adressService.updateByAddressId(adress);
    }

}
